package com.renwei.yunlong.activity.work;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.CommonBean;
import com.renwei.yunlong.event.WorkPageRefreshEvent;
import com.renwei.yunlong.global.AppHelper;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.view.SimpleOptionView;
import com.renwei.yunlong.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

/**
 * 取消--页面
 * <p>
 * Created by Administrator on 2017/8/29.
 */

public class WorkCancelActivity extends BaseActivity implements View.OnClickListener, HttpTaskListener, TextWatcher {


    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.tv_input_title)
    TextView tvInputTitle;
    @Bind(R.id.et_input_content)
    EditText etInputContent;
    @Bind(R.id.tv_input_number)
    TextView tvInputNumber;
    @Bind(R.id.btn_confirm)
    Button btnConfirm;
    private String pager;
    private String requestId;
    private String eventId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cancel);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        pager = getIntent().getStringExtra("pager");
        requestId = getIntent().getStringExtra("requestId");
        eventId = getIntent().getStringExtra("eventId");

        initData();
    }

    private void initData() {
        btnConfirm.setOnClickListener(this);
        pager = getIntent().getStringExtra("pager");
        if ("取消".equals(pager)) {
            simpleTileView.setTitle("取消");
            tvInputTitle.setText("取消原因");
        } else if ("挂起".equals(pager)) {
            simpleTileView.setTitle("挂起");
            tvInputTitle.setText("挂起原因");
        } else if ("启动".equals(pager)) {
            simpleTileView.setTitle("启动");
            tvInputTitle.setText("启动原因");
        }
        etInputContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(100), AppHelper.emojiFilter});
        etInputContent.addTextChangedListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_confirm:
                String content = etInputContent.getText().toString().trim();
                HashMap<String, String> map = new HashMap<>();
                if ("取消".equals(pager)) {
                    if ("1".equals(companyType)) {
                        map.put("currentUserId", ownerBean.getRows().getEmployeeId());
                    } else if ("2".equals(companyType)) {
                        map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
                    }
                    map.put("requestIds", requestId);
                    map.put("reponseDesc", content);
                    map.put("nowStat", "11");
                    ServiceRequestManager.getManager().setWorkClose(this, JsonMapUtil.mapToJson(map), this);
                } else if ("启动".equals(pager) || "挂起".equals(pager)) {
                    if ("1".equals(companyType)) {
                        map.put("requestId", requestId);
                        map.put("eventId", eventId);
                        map.put("eventState", "挂起".equals(pager) ? "7" : "8");
                        map.put("note", content);
                        map.put("ownerCode", ownerBean.getRows().getOwnerCode());
                        map.put("currentUserId", ownerBean.getRows().getEmployeeId());
                    } else if ("2".equals(companyType)) {
                        map.put("requestId", requestId);
                        map.put("eventId", eventId);
                        map.put("eventState", "挂起".equals(pager) ? "7" : "8");
                        map.put("note", content);
                        map.put("serviceProviderCode", serviceLoginBean.getRows().getServiceProviderCode());
                        map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
                    }
                    ServiceRequestManager.getManager().setWorkStart(this, JsonMapUtil.mapToJson(map), this);

                }
                break;
            default:
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence s, int i, int i1, int i2) {
        int length = s.length();
        tvInputNumber.setText(length + "");
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                EventBus.getDefault().post(new WorkPageRefreshEvent());
                CommonBean cancelBean = new Gson().fromJson(result, CommonBean.class);
                if (cancelBean.getMessage().getCode() == 200) {
                    showCenterSuccessMsg("操作成功");
                    finish();
                } else if (cancelBean.getMessage().getCode() == 1008) {
                    showCenterInfoMsg("无权限访问");
                } else if (cancelBean.getMessage().getCode() == 1029) {
                    showCenterInfoMsg("此工单已被其他人操作，不能进行此操作!");
                } else {
                    showCenterInfoMsg(cancelBean.getMessage().getMessage());
                }
                break;
            case 2:
                EventBus.getDefault().post(new WorkPageRefreshEvent());
                CommonBean startBean = new Gson().fromJson(result, CommonBean.class);
                if (startBean.getMessage().getCode() == 200) {
                    showCenterSuccessMsg("操作成功");
                    finish();
                } else {
                    showCenterInfoMsg(startBean.getMessage().getMessage());
                }
                break;
                default:
                break;
        }
    }

    @Override    public void onException(int requestId, String code) {        showTopWrongMsg(getResources().getString(R.string.net_work_error));    }

    public static void OpenActivity(Context context, String requestId, String eventId, String pager) {
        Intent i = new Intent(context, WorkCancelActivity.class);
        i.putExtra("requestId", requestId);
        i.putExtra("pager", pager);
        i.putExtra("eventId", eventId);
        context.startActivity(i);
    }

}
